IO流介绍
输入流
InputStream
FIleInputStream
BufferedInputStream
ObjectInputStream
Reader
FileReader
BufferedReader
InputStreamReader
输出流
OutputStream
FileOutputStream
BufferedOutputStream
ObjectOutputStream
Writer
FileWriter
BufferedWriter
OutputStreamWriter
文件流:文件在程序中是以
流
的形式进行操作的
流:
数据在数据源(文件)和程序(内存)之间经历的路径
输入流:
数据从数据源(人间)到程序(内存)的路径
输出流:
数据从程序(内存)到数据源(文件)的路径
常用文件操作
创建文件
new File(String pathname); #根据路径构建一个Fil对象
new File(File parent, String child); #根据父目录文件 + 子路径构建
new File(String parent, String child); #根据父目录 + 子路径构建
file.creatNewFile #创建新文件
案例
private static final String path = "/Users/Jeffrey/Public/";
@Test
public void createdFileTypeTest1(){
String fileName = "a.txt";
try {
File file = new File(path + fileName);
boolean newFile = file.createNewFile();
if (newFile){
System.out.println("success");
} else {
System.out.println("error");
}
}catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
@Test
public void createdFileTypeTest3(){
String fileName = "a4.txt";
File file = new File(new File(path),fileName);
try {
boolean newFile = file.createNewFile();
if (newFile){
System.out.println("success");
} else {
System.out.println("error");
}
}catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
@Test
public void createdFileTypeTest2(){
String fileName = "a1.txt";
try {
File file = new File(path,fileName);
boolean newFile = file.createNewFile();
if (newFile){
System.out.println("success");
} else {
System.out.println("error");
}
}catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
读取文件
gatName
getAbsolutePath #绝对路径
getParent
length
exists
isFile
isDirectory
案例
private static final String path = "/Users/Jeffrey/Public/";
@Test
public void getInfo(){
String fileName = path + "a.txt";
File file = new File(fileName);
System.out.println("文件名 :" + file.getName());
System.out.println("文件绝对路径 :" + file.getAbsolutePath());
System.out.println("文件上级目录 :" + file.getParent());
System.out.println("文件大小(字节) :" + file.length());
System.out.println("文件存不存在 :" + file.exists());
System.out.println("文件是不是文件 :" + file.isFile());
System.out.println("文件是不是文件夹 :" + file.isDirectory());
}
#=============打印==============
> 文件名 :a.txt
> 文件绝对路径 :/Users/Jeffrey/Public/a.txt
> 文件上级目录 :/Users/Jeffrey/Public
> 文件大小(字节) :0
> 文件存不存在 :true
> 文件是不是文件 :true
> 文件是不是文件夹 :false
IO流原理及分类
流的分类
按操作数据单位不同分为: 字节流(8 bit)、字符流(按字符)
按数据流的流向不同分为:输入流、输出流
按流的角色的不同分为:字节流、处理流/包装流
| (抽象基类) | 字符流 | 字节流 | | :———-: | :———-: | :———-: | | 输入流 | Reader | InputStream | | 输出流 | Writer | OutputStream |
IO流常用的类
InputStrem
字节输入流
InputStream
抽象类是所有类字节输入流的超类
InputStream
常用的子类:
FileInputStream
:文件输入流
BufferedInputStream
:缓存字节输入流
ObjectInputStream
:对象字节输入流
private static final String path = "/Users/Jeffrey/Public/a.txt";
/**
* 这个方法效率较低
* ->优化: 使用 read(byte[] b)
*/
@Test
public void readFile1(){
int readChar;
try (FileInputStream fileInputStream = new FileInputStream(path)){
while ((readChar = fileInputStream.read()) != -1){
System.out.print((char) readChar);
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 使用 read(byte[] b) 增强效率
*/
@Test
public void readFile2(){
byte[] buf = new byte[8];
int readLen;
try (FileInputStream fileInputStream = new FileInputStream(path)){
while ((readLen = fileInputStream.read(buf)) != -1){
System.out.println(new String(buf,0,readLen));
}
}catch (IOException e){
e.printStackTrace();
}
}
OutputStrem
字节输入流
OutputStream
抽象类是所有类字节输出流的超类
OutputStream
常用的子类:
FileOutputStream
:文件输出流BufferedOutputStream
:缓存字节输出流
文档更新时间: 2023-12-18 03:25 作者:JeffreyCheung